<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
require_once ('../../includes/classes/core.php');
$action 	= $_REQUEST['act'];
$data 		= '';
$error		= '';
$client_id	= $_REQUEST['id'];
$tab 		= $_REQUEST['tab'];
$month 		= $_REQUEST['month'];
$year       = $_REQUEST[year];
if ($month!='') {
	$month= "AND MONTH(IFNULL(a_letter.fact_end_date,d_letter.fact_end_date))=$month";
}
if ($year!='') {
    $year= "AND YEAR(IFNULL(a_letter.fact_end_date,d_letter.fact_end_date))=$year";
}
switch ($action) {
	case 'chart':
			if ($tab==1) {
				$result = mysql_query("SELECT
                                        			client.`name`,			
                                        			SUM(COALESCE(e_letter_details.price,b_letter_details.price,0.00))
                                        FROM	warehouse_detail
                                        LEFT JOIN b_letter_details ON b_letter_details.warehouse_detail_id=warehouse_detail.id AND warehouse_detail.barcode=b_letter_details.barcode
                                        LEFT JOIN e_letter_details ON e_letter_details.warehouse_detail_id=warehouse_detail.id AND warehouse_detail.barcode=e_letter_details.barcode
                                        LEFT JOIN	a_letter ON `a_letter`.`status` >3 AND b_letter_details.a_letter_id =a_letter.id
                                        LEFT JOIN	d_letter ON `d_letter`.`status` >3 AND e_letter_details.d_letter_id =d_letter.id
                                        JOIN      client_objects  ON client_objects.id=IFNULL(d_letter.client_object_id,a_letter.client_object_id)
                                        JOIN      client ON client_objects.client_id=client.`id`
                                        WHERE warehouse_detail.actived=1 AND warehouse_detail.type!=2 AND (NOT ISNULL(b_letter_details.id) or NOT ISNULL(e_letter_details.id))  $month $year
                                        GROUP BY client_objects.client_id");

				$rows = array();
				while($r = mysql_fetch_array($result)) {
					$row[0] = $r[0];
					$row[1] = (float) $r[1];
					$rows['data'][]=$row;
				}
				$rows['text']='თვებიის მიხედვით';
				echo json_encode($rows);
		return 0;

			}
		$prod		= $_REQUEST['prod'];
		$p="(NOT ISNULL(warehouse.a_letter_id) or NOT ISNULL(warehouse.d_letter_id))";
		$client		= $_REQUEST['client'];
		if ($client!=0){$c="AND client_objects.client_id=$client";}
		$p="true";
		if ($prod==1){$p="NOT ISNULL(b_letter_details.id)";}
		if ($prod==2){$p="NOT ISNULL(e_letter_details.id)";}
////////////////////////////---------------------------------------------------------------------
		$sth = mysql_query("
		          SELECT  MONTH(IFNULL(a_letter.fact_end_date,d_letter.fact_end_date)) AS `mont`,
                		  DATE_FORMAT(IFNULL(a_letter.fact_end_date,d_letter.fact_end_date),'%b') AS `mont1`,
                		  count(*) as qv,
                		  '',
                		  SUM(COALESCE(e_letter_details.price,b_letter_details.price,0.00))as price
                FROM
                warehouse_detail
                LEFT JOIN b_letter_details ON b_letter_details.warehouse_detail_id=warehouse_detail.id AND warehouse_detail.barcode=b_letter_details.barcode
                LEFT JOIN e_letter_details ON e_letter_details.warehouse_detail_id=warehouse_detail.id AND warehouse_detail.barcode=e_letter_details.barcode
                LEFT JOIN	a_letter ON `a_letter`.`status` >3 AND b_letter_details.a_letter_id =a_letter.id
                LEFT JOIN	d_letter ON `d_letter`.`status` >3 AND e_letter_details.d_letter_id =d_letter.id
                JOIN      client_objects  ON client_objects.id=IFNULL(d_letter.client_object_id,a_letter.client_object_id)
                WHERE warehouse_detail.actived=1 AND warehouse_detail.type!=2  
                	  AND $p $c $year
                GROUP BY mont");
		$rows1 = array();
		$rows = array();
		$rows2 = array();
		$rows4 = array();
		$rows['name'] = 'თანხა';
		$rows2['name'] = 'რაოდენობა';
		while($r = mysql_fetch_array($sth)) {
			$rows['data'][] = $r['price'];
			$rows2['data'][] = $r['qv'];
			$rows1[] = $r['mont1'];
		}
		while($r1 = mysql_fetch_array($rResult)) {
			$rows3['data'][] = $r1;
			$rows4[] = $r1[0];

		}


		$result = array();
		$result[d2]=$result[n]=$result[d]=$result[d3]=array();
		$result[m]=array();
		array_push($result[d],$rows2);
		array_push($result[d2],$rows);
		array_push($result[m],$rows1);
		array_push($result[n],$rows4);
		array_push($result[d3],$rows3);
		print json_encode($result, JSON_NUMERIC_CHECK);
		return 0;
		//------------------------------------------------------------------------------------
		break;
	case 'client':

		$rResult = mysql_query("SELECT id, `name`
						FROM `client`
						WHERE actived=1");
		$data = array(
				"aaData"	=> array()
		);
		$data['aaData'][] = "<option value='0'>აირჩიეთ კლიენტი</option>";
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{

			$data['aaData'][] = "<option value='$aRow[0]'>$aRow[1] </option>";
		}
	break;
	case 'get_list':
		$prod		= $_REQUEST['prod'];
		$p="(NOT ISNULL(warehouse.a_letter_id) or NOT ISNULL(warehouse.d_letter_id))";
		$client		= $_REQUEST['client'];
		if ($client!=0){$c="AND client_objects.client_id=$client";}
		$p="true";
		if ($prod==1){$p="NOT ISNULL(warehouse.a_letter_id) ";}
		if ($prod==2){$p="NOT ISNULL(warehouse.d_letter_id)";}
		//$i=0;
if ($tab==0) {

		$rResult = mysql_query("
		    	SELECT  MONTH(IFNULL(a_letter.fact_end_date,d_letter.fact_end_date)) AS `mont`,
                		DATE_FORMAT(IFNULL(a_letter.fact_end_date,d_letter.fact_end_date),'%b') AS `mont1`,
                		count(*) as qv,
                		'',
                		SUM(COALESCE(e_letter_details.price,b_letter_details.price,0.00))as price
                FROM
                warehouse_detail
                LEFT JOIN b_letter_details ON b_letter_details.warehouse_detail_id=warehouse_detail.id AND warehouse_detail.barcode=b_letter_details.barcode
                LEFT JOIN e_letter_details ON e_letter_details.warehouse_detail_id=warehouse_detail.id AND warehouse_detail.barcode=e_letter_details.barcode
                LEFT JOIN	a_letter ON `a_letter`.`status` >3 AND b_letter_details.a_letter_id =a_letter.id
                LEFT JOIN	d_letter ON `d_letter`.`status` >3 AND e_letter_details.d_letter_id =d_letter.id
                JOIN      client_objects  ON client_objects.id=IFNULL(d_letter.client_object_id,a_letter.client_object_id)
                WHERE warehouse_detail.actived=1 AND warehouse_detail.type!=2  
                	  AND $p $c $year
                GROUP BY mont");
		$data = array(
				"aaData"	=> array()
		);
		$aRow2=array(
				"aaData"	=> array()
		);
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$i++;
			$aRow1=$aRow;
			$aRow1[3]=$aRow1[2]-$aRow2[2];
			$aRow1[5]=$aRow1[4]-$aRow2[4];
			$data['aaData'][]=$aRow1;
			$aRow2=$aRow1;

		}

}
else {
	$rResult = mysql_query("SELECT 	client.`id`,	    
                                	client.`name`,			
                        			SUM(COALESCE(e_letter_details.price,b_letter_details.price,0.00))
                        FROM	warehouse_detail
                        LEFT JOIN b_letter_details ON b_letter_details.warehouse_detail_id=warehouse_detail.id AND warehouse_detail.barcode=b_letter_details.barcode
                        LEFT JOIN e_letter_details ON e_letter_details.warehouse_detail_id=warehouse_detail.id AND warehouse_detail.barcode=e_letter_details.barcode
                        LEFT JOIN	a_letter ON `a_letter`.`status` >3 AND b_letter_details.a_letter_id =a_letter.id
                        LEFT JOIN	d_letter ON `d_letter`.`status` >3 AND e_letter_details.d_letter_id =d_letter.id
                        JOIN      client_objects  ON client_objects.id=IFNULL(d_letter.client_object_id,a_letter.client_object_id)
                        JOIN      client ON client_objects.client_id=client.`id`
                        WHERE warehouse_detail.actived=1 AND warehouse_detail.type!=2 AND (NOT ISNULL(b_letter_details.id) or NOT ISNULL(e_letter_details.id))  $month $year
                        GROUP BY client_objects.client_id
");
			$data = array(
					"aaData"	=> array()
			);
			$aRow2=array(
					"aaData"	=> array()
			);
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
			//$i++;
				$rows['name'] = 'თანხა';
				$rows2['name'] = 'რაოდენობა';
			$data['aaData'][]=$aRow;

			//$aRow2=$aRow1;

	}



}
		break;
    default:
       $error = "action null";
}

$data['error'] = $error;

echo json_encode($data);

?>